package scene.gameObject 
{
	import flash.geom.Point;
	import scene.map.Map;
	import scene.scene.SceneData;
	/**
	 * ...
	 * @author carlor
	 */
	public class GameObjectManager 
	{
		private var _owner	 : SceneData;
		private var _charList: Object;
		private var _npcList : Object;
		
		public function GameObjectManager(owner: SceneData) 
		{
			_owner    = owner;
			_charList = new Object();
			_npcList  = new Object();
		}
		
		public function dispose(): void
		{
			
		}
		
		public function update(curTime: int): void
		{
			for each (var charObj: CharObject in _charList)
				charObj.update(curTime);
			for each (var npcObj: NPCObject in _npcList)
				npcObj.update(curTime);
		}
		
		public function resize(obj: BasicObject = null): void
		{
			for each (var charObj: CharObject in _charList) {
				if (charObj != obj)
					charObj.resize();
			}
			for each (var npcObj: NPCObject in _npcList) {
				if (npcObj != obj)
					npcObj.resize();			
			}
		}
		
		public function addChar(_map: Map, fullInfo: Object, data: Object, settingData: Object): CharObject
		{
			if (!_charList[data.id]) {
				var obj: CharObject = new CharObject(_map, _owner, data, settingData);
				_charList[data.id] = obj;
				addMapBlock(data.mapPos, fullInfo.camp);
				addMapBlockX(obj);
			}
			return _charList[data.id];
		}
		
		public function deleteChar(id: String): CharObject
		{
			if (_charList[id]) {
				var charObj: CharObject = _charList[id];
				deleteMapBlock(charObj.basicData.mapPos);
				delete _charList[id];
				return charObj;
			}
			return null;
		}
		
		public function findObject(id: String): BasicObject
		{
			if (_charList[id]) return _charList[id];
			else if (_npcList[id]) return _npcList[id];
			else return null;
		}
		
		public function pickObject(pos: Point): BasicObject
		{
			var key: Object;
			var obj: BasicObject;
			
			for (key in _charList) {
				obj = _charList[key];
				if (obj is ActivateObject && (ActivateObject(obj).objectInfo.curHp <= 0)) continue;
				if (obj.basicData.mapPos.x == pos.x && obj.basicData.mapPos.y == pos.y)
					return obj;
			}
			for (key in _npcList) {
				obj = _npcList[key];
				if (obj is ActivateObject && (ActivateObject(obj).objectInfo.curHp <= 0)) continue;
				if (obj.basicData.mapPos.x == pos.x && obj.basicData.mapPos.y == pos.y)
					return obj;
			}			
			
			return null;
		}
		
		public function clearObject(): void
		{
			var key: *;
			var obj: BasicObject;
			var list: Array = [];
			
			for (key in _charList)
				list.push(key);
			while (list.length > 0) {
				key = list.pop();
				obj = _charList[key];
				obj.dispose();
				obj = null;				
				delete _charList[key];	
				key = null;
			}
		}
		
		private function addMapBlockX(newObj: ActivateObject): void
		{
			var key: Object;
			var obj: ActivateObject;			
			for (key in _charList) {
				obj = _charList[key];
				if (newObj == obj) continue;
				newObj.addBlock(obj.basicData.mapPos, obj.objectInfo.camp);
			}
			
			for (key in _npcList) {
				obj = _npcList[key];
				if (newObj == obj) continue;
				newObj.addBlock(obj.basicData.mapPos, obj.objectInfo.camp);
			}			
		}

		public function addMapBlock(pos: Point, camp: int): void
		{
			var key: Object;
			var obj: ActivateObject;			
			for (key in _charList) {
				obj = _charList[key];
				obj.addBlock(pos, camp);
			}
			
			for (key in _npcList) {
				obj = _npcList[key];
				obj.addBlock(pos, camp);
			}
		}
		
		public function deleteMapBlock(pos: Point): void
		{
			var key: Object;
			var obj: ActivateObject;			
			for (key in _charList) {
				obj = _charList[key];
				obj.deleteBlock(pos);
			}
			
			for (key in _npcList) {
				obj = _npcList[key];
				obj.deleteBlock(pos);
			}
		}	
		
		public function get charList(): Object
		{
			return _charList;
		}
		
	}

}